<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
		xmlns:trans="http://www.thomaso.com/xmlns/bridge/trans"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
		xmlns:bridge="http://www.thomaso.com/xmlns/bridge"
		xmlns:common="http://exslt.org/common"
                extension-element-prefixes="common"
                exclude-result-prefixes="trans bridge">


<xsl:template name="call">
<xsl:param name="context"/>
<xsl:param name="code"/>
<xsl:value-of select="$code"/>
</xsl:template>

<!-- This adds "__empty__" calls to the beginning of the auction -->
<xsl:template name="addemptycalls">
<xsl:param name="calls"/>
<xsl:param name="column"/>
<xsl:param name="seatorder"/>
<xsl:param name="gap" select="0"/>

<xsl:copy-of select="$translations/trans:gaps/bridge:call[$gap>=position()]"/>

<xsl:variable name="colnumber" select="($column+$gap) mod 4"/>
<xsl:variable name="expectedseat" select="$seatorder[$colnumber+1]/@id"/>

<xsl:if test="$calls">
  <xsl:variable name="nextitem" select="$calls[1]"/>
  <xsl:variable name="rest" select="$calls[position()>1]"/>

  <xsl:copy-of select="$nextitem"/>

  <xsl:call-template name="addemptycalls">
    <xsl:with-param name="calls" select="$rest"/>
    <xsl:with-param name="column" select="($colnumber+1) mod 4"/>
    <xsl:with-param name="seatorder" select="$seatorder"/>
  </xsl:call-template>
</xsl:if> 
</xsl:template>

<xsl:template name="normalizecalls">
<xsl:param name="calls"/>

<xsl:variable name="callcount" select="count($calls[self::bridge:call])"/>
<xsl:variable name="realcount" select="count($calls[self::bridge:call and not(@code='_empty_')])"/>
<xsl:variable name="entrycount" select="count($calls)"/>

<xsl:choose>

<!-- 
     When the last element of the auction is an annotation, normalize
     all but last, and copy the annotation
 -->
<xsl:when test="$calls[position()=$entrycount][self::bridge:annotation]">

<xsl:variable name="previous">
<xsl:call-template name="normalizecalls">
<xsl:with-param name="calls" select="$calls[position()&lt;last()]"/>
</xsl:call-template>
</xsl:variable>

<xsl:copy-of select="$previous"/>

<xsl:variable name="realcallcount" 
	select="count(common:node-set($previous)/bridge:call) mod 4"/>

<xsl:if test="$callcount>0">
<xsl:copy-of select="$translations/trans:gaps/bridge:call[position()>$realcallcount]"/>
</xsl:if>

<xsl:copy-of select="$calls[position()=last()]"/>

</xsl:when>

<!-- When the auction is a passout auction, and there are no
     notes or comments in any of the bids -->
<xsl:when 
    test="($realcount=4) and not($calls[position()>last()-4][not(self::bridge:call) or @code!='P' or @comment or bridge:note])">

<xsl:copy-of select="$calls[last()>=position()+4]"/>
<xsl:copy-of select="$translations/trans:call[@id='_PO_']/bridge:call"/>

</xsl:when>

<!-- When there are less than four bids, or when the last three 
     bids are not unadorned passes, show all calls -->
<xsl:when
    test="($realcount&lt;4) or $calls[(position()> last() - 3) and (@code!='P' or @comment or bridge:note or self::bridge:annotation)]">
<xsl:copy-of select="$calls"/>
</xsl:when>

<!-- Now, we know that the last three calls are unadorned passes
     If the auction is length four and starts with a pass, then we
     want to show all calls -->
<xsl:when test="($calls[1][@code='P'][not(@comment) or not(bridge:note)]) and ($callcount=4)">
<xsl:copy-of select="$calls"/>
</xsl:when>


<!-- It ends with three passes - make sure that it is not a case
     where a bid out of turn caused the auction to revert -->
<xsl:when test="$calls[last()-3][@code='_skip_']">
<xsl:copy-of select="$calls"/>
</xsl:when>

<xsl:when test="$calls[last()-4][@code='_skip_'] and $calls[last()-3][@code='P']">
<xsl:copy-of select="$calls"/>
</xsl:when>

<xsl:when test="$calls[last()-5][@code='_skip_'] and $calls[last()-4][@code='P'] and $calls[last()-3][@code='P']">
<xsl:copy-of select="$calls"/>
</xsl:when>

   <!-- Ends with three unadorned passes, not a pass-out auction, then
     replace the last three calls with "All Pass" -->
<xsl:otherwise>
<xsl:copy-of select="($calls[position()&lt;$entrycount - 2])|($translations/trans:call[@id='_AP_']/bridge:call)"/>
</xsl:otherwise>

</xsl:choose>

</xsl:template>

<xsl:template name="auctioncall">
  <xsl:apply-templates select="."/>
</xsl:template>

<xsl:template match="bridge:call">
<xsl:call-template name="call">
<xsl:with-param name="context" select="@context"/>
<xsl:with-param name="code" select="@code"/>
</xsl:call-template>
<xsl:value-of select="@comment"/>
</xsl:template>

<xsl:template match="bridge:auction">
<xsl:variable name="dealer" 
    select="(@dealer|(bridge:call[1]/@seat)|$translations/trans:auctions/@defaultdealer)[1]"/>
<xsl:variable name="left" select="$translations/trans:leftmost[@dealer=$dealer]/@left"/>
<xsl:variable name="gap" select="$translations/trans:leftmost[@dealer=$dealer]/@gap"/>
<xsl:variable name="leftseat" select="$translations/trans:seats/trans:seat[@id=$left][1]"/>
<xsl:variable name="seats" select="$leftseat|($leftseat/following-sibling::trans:seat[4>position()])"/>
<xsl:variable name="show" select="@show"/>
<xsl:variable name="callcount" select="count(bridge:call)"/>

<xsl:variable name="withemptycalls">
  <xsl:call-template name="addemptycalls">
    <xsl:with-param name="calls" select="bridge:call"/>
    <xsl:with-param name="seatorder" select="$seats"/>
    <xsl:with-param name="column" select="0"/>
    <xsl:with-param name="gap" select="$gap"/>
  </xsl:call-template>
</xsl:variable>


<table class="auctiontable"> 
  <xsl:variable name="normalized">
    <xsl:call-template name="normalizecalls">
      <xsl:with-param name="calls" select="common:node-set($withemptycalls)/*"/>
    </xsl:call-template>
  </xsl:variable>


  <xsl:call-template name="auction">
    <xsl:with-param name="gap" select="0"/>
    <xsl:with-param name="calls" select="common:node-set($normalized)/*"/>
  </xsl:call-template>
</table>

</xsl:template>

<xsl:template name="auction">
<xsl:param name="gap"/>
<xsl:param name="calls"/>

  <xsl:if test="count($calls)>0">
    <tr>
      <xsl:if test="$gap>0">
        <td colspan="{$gap}"></td>
      </xsl:if>
      <xsl:for-each select="$calls[4>=$gap+position()]">
        <td><xsl:apply-templates select="."/></td>
      </xsl:for-each>
    </tr>

    <xsl:call-template name="auction">
      <xsl:with-param name="gap" select="0"/>
      <xsl:with-param name="calls" select="$calls[$gap+position()>4]"/>
    </xsl:call-template>
  </xsl:if>

</xsl:template>

</xsl:stylesheet>
